<template>
	<view class="content" v-show="show">
		<!-- 顶部滚动通知 -->
		<view>
			<u-notice-bar :text="noticeBar" direction="column" duration="5000" mode="closable"></u-notice-bar>
		</view>
		<!-- 顶部位置，搜索框 -->
		<view class="head-input">
			<u-search @click="toSearch()" disabled inputAlign="center" :show-action="false" searchIconColor="#f18b49" borderColor="#f18b49" bgColor="#ffffff" placeholder="搜索你要购买的商品"></u-search>
		</view>
		<!-- 首页顶部轮播图 -->
		<view class="head-img">
			<swiper class="swiper-box" indicator-dots="#cccccc" indicator-active-color="#f18b49" circular autoplay interval="3000">
				<swiper-item class="swiper-item" v-for="(item,i) in swiperList" :key="i">
					<!-- <image :src="baseUrl + item.img" mode="widthFix" @click="toProductDetail(item.url)"></image> -->
					<image :src="baseUrl + item.img" mode="widthFix"></image>
				</swiper-item>
			</swiper>
		</view>
		<!-- 轮播图下方标签 -->
		<view class="bot-tag">
			<view class="tag-box" v-for="(item,i) in tagList" :key="i">
				<image :src="item.img" mode=""></image>
				<view class="tag-cont">{{ item.cont }}</view>
			</view>
		</view>
		<!-- 产品分类 -->
		<view class="class-content">
			<view class="class-cont-box">
				<cg-swiper :swiperList="classList" @clickItem="clickItem"></cg-swiper>
			</view>
		</view>
		<!-- 热销产品 -->
		<view class="hot-sale">
			<view class="hot-head-box">
				<view class="hot-head">
					<view class="hot-text">
						<view class="hot-img-box">
							<image src="../../static/index/hot.png" mode=""></image>
							<text class="hot-t-1">爆款产品</text>
						</view>
						<text class="hot-t-2">| 大家都在买</text>
					</view>
					<navigator open-type="switchTab" url="/pages/class/class" class="hot-btn">
						<view class="hot-btn-txt">更多</view>
						<uni-icons type="right" size="15" color="#f18b49"></uni-icons>
					</navigator>
				</view>
			</view>
			<view class="hot-product">
				<scroll-view scroll-x="true" class="scroll-view" @scrolltolower="lower" style="touch-action: none;">
					<block v-for="(item,index) in hotList" :key="index">
						<view class="item">

							<image :src="baseUrl + item.img" @click="toProductDetail(item.id)"></image>
							<view class="cont-box" @click="toProductDetail(item.id)">
								<view class="btn-b">爆款</view>
								<view class="cont-b">{{ item.name }}</view>
							</view>
							<view class="price-box">
								<view class="price-b">
									<text style="font-size: 24rpx;">￥</text>
									{{ item.sell_price }}
								</view>
								<image src="../../static/index/cart.png" @click="addCart(item.id)"></image>
							</view>
						</view>
					</block>
				</scroll-view>
				<view class="hotpro-bottom">
					<u-icon name="arrow-left-double" color="#c0c0c0" size="16"></u-icon>
					<text>可向左滑动</text>
				</view>
			</view>
		</view>
		<!-- 今日推荐 -->
		<view class="recommend">
			<view class="recommend-box">
				<view class="today-recommend">
					<view class="today-v1">推荐产品</view>
					<view class="today-v2" style="margin: 10rpx 0 25rpx 0;">全民口碑推荐</view>
					<view class="today-img-box">
						<view class="today-img" v-for="(item,index) in commendList" :key="index">
							<image :src="baseUrl+item.img" @click="toProductDetail(item.id)"></image>
							<view class="today-v3">
								<text style="font-size: 24rpx;">￥</text>
								{{ item.sell_price }}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bot-box" v-show="token == ''">
			<text>登录即可享受更多优惠,点击登录</text>
			<view @click="login">登录</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				userId: '',
				show: false,
				hotList: [],
				noticeBar: [],
				classList: [],
				swiperList: [],
				commendList: [],
				indicator: false,
				baseUrl: 'https://gjc.hy-dzsc.cn/',
				tagList: [{
						img: '../../static/index/real.png',
						cont: '正品保障'
					},
					{
						img: '../../static/index/self.png',
						cont: '官方采购'
					},
					{
						img: '../../static/index/express.png',
						cont: '快递配送'
					},
					{
						img: '../../static/index/service.png',
						cont: '售后无忧'
					}
				],
			}
		},
		onLoad() {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
			setTimeout(_ => {
			  uni.hideLoading()
				this.allAjax()
				this.show = true
			}, 400)
		},
		onShow() {
			this.token = uni.getStorageSync('token')
			this.userId = uni.getStorageSync('userid')
		},
		onPullDownRefresh() {
			setTimeout(() => {
				this.allAjax()
				uni.stopPullDownRefresh()
			},800)
		},
		methods: {
			// this.$axios.post('/gjcApi/getTest',data,res=>{
			// 	console.log(res)
			// })
			// 进入页面时加载的请求
			allAjax() {
				this.geOrderUser()
				this.getSwiper()
				this.getclassList()
				this.gethotList()
				this.getcommendList()
			},
			// 获取首页轮播图
			getSwiper() {
				this.$getHttp('/gjcApi/getBanner', {}, res => {
					this.swiperList = res
				})
			},
			// 获取下单用户
			geOrderUser() {
				this.$getHttp('/gjcApi/recentlyOrder', {}, res => {
					res.forEach(v => {
						this.noticeBar.push(`用户${v.accept_name}最近下单了`)
					})
				})
			},
			// 获取首页分类导航
			getclassList() {
				this.$getHttp('/gjcApi/getCatHead', {}, res => {
					this.classList = res
					this.classList.forEach((v, i) => {
						v.url = "/pages/class/class?current=" + i
					})
				})
			},
			// 跳转分类导航
			clickItem(e) {
				uni.reLaunch({
					url: e.url
				})
			},
			// 获取热销产品
			gethotList() {
				this.$getHttp('/gjcApi/getRenXiao', {user_id: this.userId}, res => {
					this.hotList = res
				})
			},
			// 热销产品 滑到最右边提示
			lower() {
				uni.showToast({
					title: '点击更多查看其他热销产品',
					duration: 1500,
					icon: 'none'
				})
			},
			// 获取今日推荐
			getcommendList() {
				this.$getHttp('/gjcApi/getRecommend', {user_id: this.userId}, res => {
					this.commendList = res
				})
			},
			// 跳转商品详情
			toProductDetail(id){
				uni.navigateTo({
					url: `/pages/class/product-detail?proId=${id}&userId=${this.userId}`
				})
			},
			// 跳转搜索页
			toSearch(){
				uni.navigateTo({
					url: '/pages/index/search?userId=' + this.userId
				})
			},
			// 添加购物车
			addCart(id){
				if(this.token){
					this.$getHttp('/gjcApi/joinCart?goods_id=' + id + '&type=goods&goods_num=1', { user_id: this.userId }, res => {
						if(res.isError == 406){
							uni.showToast({
								title: '请先选择商品规格参数',
								icon: 'none'
							})
							setTimeout(() => {
								uni.hideToast()
								this.toProductDetail(id)
							},1000)
						} else if(res.isError == 407){
							uni.showToast({
								title: '该商品库存不足，再等等',
								duration: 1500,
								icon: 'none'
							})
						} else if(res.isError == 408){
							uni.showToast({
								title: '加入购物车失败，已超出购物车容量',
								duration: 1500,
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '已加入购物车',
								duration: 1000,
								icon: 'none'
							})
						}
					})
				} else {
					uni.showModal({
						title: '登录验证',
						cancelText: '先逛逛',
						confirmText: '去登录',
						confirmColor: '#f18b49',
						content: '账号未登录，无法添加到购物车！',
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/code-login'
								})
							}
						}
					})
				}
			},
			// 前往登录
			login(){
				uni.navigateTo({
					url: '/pages/login/login'
					// url: '/pages/login/code-login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding-bottom: 20rpx;
		.head-input{
			padding: 20rpx 15rpx 0 15rpx;
			background: $uni-model-color;
		}

		.head-img {
			padding: 20rpx 15rpx;
			background: $uni-model-color;
			.swiper-box {
				height: 420rpx;
				overflow: hidden;
				border-radius: 20rpx;
				border: 2rpx solid $uni-text-color;
			}
			.swiper-item {
				width: calc(100vw - 60rpx);
				height: calc(50vw - 30rpx);
				image {
					width: 100%;
				}
			}
		}

		.bot-tag {
			display: flex;
			margin-bottom: 20rpx;
			background: $uni-model-color;
			padding: 0 15rpx 20rpx 15rpx;
			justify-content: space-around;
			.tag-box {
				display: flex;
				align-items: center;
				image {
					width: 36rpx;
					height: 36rpx;
					margin-top: 2rpx;
				}
				.tag-cont {
					font-size: 28rpx;
					margin-left: 5rpx;
					color: $uni-text-color-grey;
				}
			}
		}
		.class-content {
			padding: 0 15rpx;
			.class-cont-box {
				padding: 5rpx;
				border-radius: 20rpx;
				background: $uni-model-color;
				// box-shadow: 2rpx 2rpx 10rpx 0 #cccccc;
			}
		}
		.hot-sale {
			.hot-head-box {
				padding: 20rpx 15rpx;
				.hot-head {
					display: flex;
					align-items: center;
					border-radius: 20rpx;
					justify-content: space-between;
					padding: 25rpx 15rpx 50rpx 15rpx;
					background-image: linear-gradient(#ff914d, #ffc567);
					.hot-text {
						display: flex;
						align-items: flex-end;
						.hot-img-box{
							display: flex;
							align-items: center;
							image {
								width: 38rpx;
								height: 38rpx;
								padding: 6rpx;
								margin: 0 10rpx;
								border-radius: 50%;
								background: $uni-model-color;
							}
							.hot-t-1 {
								font-size: 40rpx;
								font-weight: bold;
								letter-spacing: 6rpx;
							}
						}
						.hot-t-2 {
							font-size: 26rpx;
							margin: 0 0 4rpx 10rpx;
							color: $uni-text-color-inverse;
						}
					}
					.hot-btn {
						display: flex;
						font-size: 28rpx;
						align-items: center;
						padding: 8rpx 12rpx;
						border-radius: 50rpx;
						color: $uni-text-color;
						background: $uni-text-color-inverse;
						.hot-btn-txt{
							margin-left: 12rpx;
						}
						.uniui-right{
							margin-top: 2rpx;
						}
					}
				}
			}

			.hot-product {
				margin-top: -60rpx;
				padding: 20rpx 15rpx;
				.scroll-view {
					margin: 0 auto;
					white-space: nowrap;
					background: $uni-model-color;
					border-radius: 20rpx 20rpx 0 0;
					.item {
						width: 220rpx;
						vertical-align: top;
						display: inline-block;
						padding: 20rpx 0 15rpx 25rpx;
						image {
							width: 220rpx;
							height: 220rpx;
							border-radius: 10rpx;
							border: 2rpx solid $uni-text-color;
						}
						.cont-box {
							display: flex;
							margin: 15rpx 0;
							align-items: center;
							.btn-b {
								font-size: 24rpx;
								margin-right: 6rpx;
								padding: 2rpx 10rpx;
								border-radius: 20rpx;
								color: $uni-model-color;
								background: $uni-text-color;
							}
							.cont-b {
								font-size: 30rpx;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}
						}
						.price-box {
							display: flex;
							align-items: center;
							justify-content: space-between;
							.price-b {
								font-size: 32rpx;
								color: $uni-text-color;
							}
							image {
								width: 28rpx;
								height: 28rpx;
								padding: 6rpx;
								border-radius: 50%;
								border: 2rpx solid $uni-text-color;
							}
						}
					}
				}
				.hotpro-bottom{
					display: flex;
					padding-bottom: 15rpx;
					justify-content: center;
					background: $uni-model-color;
					border-radius: 0 0 20rpx 20rpx;
					text{
						font-size: 24rpx;
						color: $uni-text-color-disable;
						
					}
				}
			}
		}

		.recommend {
			padding: 0 15rpx;
			.recommend-box {
				padding: 20rpx;
				border-radius: 20rpx;
				background: $uni-model-color;
				.today-recommend {
					border-radius: 15rpx;
					padding: 20rpx 0 20rpx 20rpx;
					background-image: linear-gradient(#ff9e4f, #fffeea);
					.today-v1 {
						font-size: 38rpx;
						font-weight: bold;
					}
					.today-v2 {
						font-size: 26rpx;
						color: $uni-text-color-inverse;
					}
					.today-img-box {
						display: flex;
						.today-img {
							overflow: hidden;
							padding-right: 25rpx;
							image {
								width: 140rpx;
								height: 140rpx;
								margin-bottom: 4rpx;
								border-radius: 10rpx;
								border: 2rpx solid $uni-text-color;
							}
							.today-v3 {
								font-size: 30rpx;
								color: $uni-text-color;
							}
						}
					}
				}
			}
		}
		.bot-box{
			opacity: .6;
			width: 750rpx;
			display: flex;
			position: fixed;
			padding: 10rpx 0;
			background: #000000;
			align-items: center;
			justify-content: space-between;
			bottom: var(--window-bottom,0);
			text{
				font-size: 30rpx;
				padding-left: 20rpx;
				color: $uni-model-color;
			}
			view{
				font-size: 28rpx;
				background: #ff914d;
				margin-right: 20rpx;
				padding: 8rpx 30rpx;
				border-radius: 50rpx;
				color: $uni-model-color;
			}
		}
	}
</style>
